$(function() {
    var audio = document.getElementById("playMusic"), myScroll, Player;

    $("#MusicList").find('li').each(function(index, val) {
        $(this).css('width', $("#page").width() + 'px');
    });
    var oLi = $("#MusicList").find('li');

    $("#play").on('click', function(event) {
        var Defaultsong = $('.music_overlay').attr('data-src'),
            SongName = $('.music_overlay').attr('song-name'),
            SongSinge = $('.music_overlay').attr('singe'),
            OldSong = $("#playMusic").attr('data-src');

        togglePlay(audio);
        if (audio.src === "") {
            $("#MusicList li").first().find('.music_overlay').addClass('active').addClass("ani-rotate");
            Player = new updatePlayer(SongName, SongSinge, Defaultsong).toPlay();
        } else {
            audio.play();
        }

    });
    //暂停
    $("#pause").on('click', function(event) {
        togglePlay(audio);
    });

    //下一曲
    $("#next").on('click', function() {
        $("#MusicList").find('.music_overlay').each(function() {
            if ($(this).hasClass('active') === true) {
                $('.music_overlay').removeClass('active').removeClass("ani-rotate");
                var Defaultsong, SongName, SongSinge, _this, OldSong;
                var isLast = $(this).parents("li").next("li").length == 0 ? true : false;
                
                if (isLast) {
                    _this = $("#MusicList li").first().find('.music_overlay');
                    Defaultsong = _this.attr('data-src');
                    SongName = _this.attr('song-name');
                    SongSinge = _this.attr('singe');
                    OldSong = $("#playMusic").attr('data-src');
                    $(this).parents("li").removeClass("active");
                    $("#MusicList li").first().addClass('active');
                } else {
                    _this = $(this).parents("li").next("li").find(".music_overlay");
                    Defaultsong = _this.attr('data-src');
                    SongName = _this.attr('song-name');
                    SongSinge = _this.attr('singe');
                    OldSong = $("#playMusic").attr('data-src');
                    $(this).parents("li").removeClass("active");
                    $(this).parents("li").next().addClass("active")
                }

                _this.addClass('active').addClass("ani-rotate");
                Player = new updatePlayer(SongName, SongSinge, Defaultsong).toPlay();
                return false;
            } else {
              $("#play").click();
            }
        })
    });
    //上一曲
    //下一曲
    $("#prev").on('click', function() {
        $("#MusicList").find('.music_overlay').each(function() {
            if ($(this).hasClass('active') === true) {
                $('.music_overlay').removeClass('active').removeClass("ani-rotate");;
                var Defaultsong, SongName, SongSinge, _this, OldSong;
                var isFirst = $(this).parents("li").prev("li").length == 0 ? true : false;
                if (isFirst) {
                    _this = $("#MusicList li").last().find('.music_overlay');
                    Defaultsong = _this.attr('data-src');
                    SongName = _this.attr('song-name');
                    SongSinge = _this.attr('singe');
                    OldSong = $("#playMusic").attr('data-src');
                    $(this).parents("li").removeClass("active");
                    $("#MusicList li").last().addClass('active');
                } else {
                    _this = $(this).parents("li").prev("li").find(".music_overlay");
                    Defaultsong = _this.attr('data-src');
                    SongName = _this.attr('song-name');
                    SongSinge = _this.attr('singe');
                    OldSong = $("#playMusic").attr('data-src');
                    $(this).parents("li").removeClass("active");
                    $(this).parents("li").prev().addClass("active");
                }

                _this.addClass('active').addClass("ani-rotate");
                Player = new updatePlayer(SongName, SongSinge, Defaultsong).toPlay();
                return false;
            } else {
              $("#play").click();
            }
        })
    });    



// 更新播放状态
function updatePlayer(name, singe, src) {　　　　
    this.name = name;　　　　
    this.singe = singe;　　　　
    this.src = src;
}
updatePlayer.prototype.toPlay = function() {
    $(".hearder h3").text(this.name);
    $("#CurrentPlayName").text(this.name + ' - ' + this.singe);
    audio.src = "./src/MP3/" + this.src ;
    $("#playMusic").attr('data-src', this.src);
    $("#playMusic").attr('data-src-flag', this.src);
    audio.play();
    
    $("#play").css('display', 'none');
    $("#pause").css('display', 'block');

    TimeSpan(audio);
};


//
/*$('#progressbar').progressbar({
    valueChange: function() {
        alert(this.value());
        //audio.currentTime(this.value());
    }
});*/
//
});

// 
function togglePlay(audioObj) {
    if (audioObj.paused === true) {
        audioObj.play();
        $("#play").css('display', 'none');
        $("#pause").css('display', 'block');
        $("#MusicList li").find('.active').addClass("ani-rotate")
    } else {
        audioObj.pause();
        $("#play").css('display', 'block');
        $("#pause").css('display', 'none');
        $("#MusicList li").find('.active').removeClass("ani-rotate")
    }
}


//时间进度处理程序

function TimeSpan(audioObj) {

    var ProcessYet = 0;

    setInterval(function() {
        
        var ProcessYet = (audioObj.currentTime / audioObj.duration) * 100;
        $('#progressbar').progressbar('value', ProcessYet);

        var currentTime = timeDispose(audioObj.currentTime);
        var timeAll = timeDispose(audioObj.duration);
        $(".SongTime.l").html(currentTime);
        $(".SongTime.r").html(timeAll);
        //播放结束更新一下播放器状态
        if (audioObj.ended === true) {
            $('#progressbar').progressbar('value', "0");
            $(".SongTime.l").html("00:00");
            $('.music_overlay').removeClass('active').removeClass("ani-rotate");
            togglePlay(audioObj)
        };

    }, 200);

}

//时间处理，因为时间是以为单位算的，所以这边执行格式处理一下

function timeDispose(number) {

    var minute = parseInt(number / 60);

    var second = parseInt(number % 60);

    minute = minute >= 10 ? minute : "0" + minute;

    second = second >= 10 ? second : "0" + second;

    return minute + ":" + second;

}

//当前歌曲的总时间

/*function TimeAll(audioObj) {
    return audioObj.duration;
}*/

